package cate.game.play.buff.h;

import cate.common.util.XT;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;

import java.util.List;

/**
 * 死亡后有30%概率（无视禁活）复活，并恢复25%生命，若在觉醒状态结束前未阵亡，为自身恢复30%生命，复生持续3回合
 *
 * 强化效果1：每次【觉醒·复生】复活后可提升自身10%攻击力(不可驱散)，最多5层
 * 强化效果2：觉醒状态下死亡且该回合没有触发【觉醒·复生】复活，则对随机3个敌方单位附加【寂灭】(不可净化)，每回合降低目标25%觉醒值，持续2回合
 */
public class 觉醒复生BH extends BuffHandler {

	private int 概率;

	private double 复活系数;

	private double 恢复系数;

	private List<Integer> 复活buff;

	private int 寂灭人数;

	private int 寂灭buff;

	//概率=3000&复活系数=2500&恢复系数=1500&复活buff=??&寂灭人数=3&寂灭buff=？？
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		概率 = args.getInt("概率", 0);
		复活系数 = args.getWDouble("复活系数", 0d);
		恢复系数 = args.getWDouble("恢复系数", 0d);
	}

	@Override
	public void parseExt(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		恢复系数 += args.getWDouble("恢复系数", 0d);
		复活buff = args.getIntList("复活buff");
		寂灭人数 = args.getInt("寂灭人数",0);
		寂灭buff = args.getInt("寂灭buff",0);
	}

	public int resurrcetRound;

	@Override
	public void immediateRebirth(ActionCtx action) {
		if (buff.owner.isAlive()) {
			return;
		}
		if (resurrcetRound > 0) {
			return;
		}
		if (!XT.chance(概率)) {
			return;
		}
		boolean success = this.buff.owner.attr.tryResurrect(action, buff.owner.attr.total().hpTotal() * 复活系数, true);
		if (success) {
			resurrcetRound = action.getRound().index;
			if (XT.isNotBlank(复活buff)) {
				复活buff.forEach(i -> buff.owner.buff.tryAddByTid(action, i));
			}
		}
	}

	@Override
	public void onDead(ActionCtx action, Fighter killer) {
		if (action.getRound().index == resurrcetRound) {
			return;
		}
		if (寂灭人数 <= 0 || 寂灭buff <= 0) {
			return;
		}
		TargetFilterParam param = new TargetFilterParam().targetNumSet(寂灭人数);
		for (Fighter target : TargetSelector.doFind(buff.owner, action.getTeamVs(), param)) {
			target.buff.tryAddByTid(action, buff.owner, 寂灭buff);
		}
	}

	@Override
	public void onInvalid(ActionCtx action, byte invalidReason) {
		if (buff.owner.isDead()) {
			return;
		}
		buff.owner.attr.buffHeal(action, buff.owner, buff.owner.attr.total().hpTotal() * 恢复系数);
	}
}
